<template>
  <a-modal :title="title" :visible="open" width="700px" :confirm-loading="submitLoading" @ok="handleSubmit" @cancel="handleClose">
    <a-form-model ref="formRef" :model="form" :rules="rules" :labelCol="{span: 4}" :wrapperCol="{span: 20}">
      <a-form-model-item label="任务标题" prop="title">
        <a-input v-model="form.title" placeholder="请输入任务标题" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="任务名称" prop="jobName">
        <a-input v-model="form.jobName" placeholder="请输入任务名称，提示：任务的Bean名称" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="任务分组" prop="jobGroup">
        <a-radio-group :default-value="form.jobGroup" v-model="form.jobGroup" button-style="solid">
          <a-radio-button value="SYSTEM">系统</a-radio-button>
          <a-radio-button value="BUSINESS">业务</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="参数" prop="params">
        <a-input v-model="form.params" placeholder="请输入参数，JSON格式" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="cron表达式" prop="cron">
        <a-input v-model="form.cron" placeholder="请输入cron表达式" allow-clear/>
      </a-form-model-item>
      <a-form-model-item label="是否启用" prop="isEnable">
        <a-radio-group :default-value="form.isEnable" v-model="form.isEnable" button-style="solid">
          <a-radio-button :value="1">启用</a-radio-button>
          <a-radio-button :value="2">禁用</a-radio-button>
        </a-radio-group>
      </a-form-model-item>
      <a-form-model-item label="备注" prop="remark">
        <a-input v-model="form.remark" placeholder="请输入备注" type="textarea" allow-clear/>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import {getJobInfo, saveJobInfo, updateJobInfo} from '@/api/quartz/jobInfo'

export default {
  data() {
    return {
      submitLoading: false,
      title: '',
      open: false,
      // 表单
      form: {},
      rules: {
        title: [{required: true, trigger: ['blur', 'change'], message: '请输入任务标题'}],
        jobName: [{required: true, trigger: ['blur', 'change'], message: '请输入任务名称'}],
        jobGroup: [{required: true, trigger: ['blur', 'change'], message: '请选择任务分组'}],
        params: [{required: false, trigger: ['blur', 'change'], message: '请输入参数'}],
        cron: [{required: true, trigger: ['blur', 'change'], message: '请输入cron表达式'}],
        isEnable: [{required: true, trigger: ['blur', 'change'], message: '请输入是否启用'}],
        remark: [{required: false, trigger: ['blur', 'change'], message: '请输入备注'}]
      }
    }
  },
  methods: {
    /** 点击关闭 */
    handleClose() {
      this.reset()
      this.open = false
    },
    /** 表单重置 */
    reset() {
      this.$refs.formRef && this.$refs.formRef.resetFields()
      this.form = {
        id: undefined,
        jobGroup: 'SYSTEM',
        beanName: undefined,
        methodName: undefined,
        params: undefined,
        cron: undefined,
        isEnable: 2,
        remark: undefined
      }
    },
    /** 点击新增 */
    handleAdd() {
      this.reset()
      this.title = '添加定时任务'
      this.open = true
    },
    /** 点击修改 */
    handleUpdate(row) {
      this.reset()
      getJobInfo(row.id).then(res => {
        this.title = '修改定时任务'
        this.open = true
        this.$nextTick(() => {
          this.form = res.data
        })
      })
    },
    /** 点击保存 */
    handleSubmit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id && this.form.id > 0) {
            updateJobInfo(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            saveJobInfo(this.form).then(res => {
              this.$message.success(res.msg)
              this.handleClose()
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        }
      })
    }
  }
}
</script>
